#!/usr/bin/env python

import pygtk
pygtk.require('2.0') # Make sure user has pygtk atleast version 2
import gtk
from subprocess import Popen, PIPE

class Main:
    def killApp(self, widget, data=None):
        print "Application terminated"
        gtk.main_quit() # Quit application
    
    # Combo function
    def combo_text(self, widget):
        self.textbox.set_text(widget.get_active_text())
    
    # Hide textbox
    def hideTextbox(self, widget):
        self.textbox.hide()
        
    # Show hidden textbox
    def showTextbox(self, widget):
        self.textbox.show()
    
    # Change title to text from textbox
    def changedText(self, widget):
        self.frame.set_title(self.textbox.get_text())
    
    # Clear text in textbox
    def clearText(self, widget):
        self.textbox.set_text("")
        
    # Speak text from speak_textbox
    def speakText(self, widget):
        self.splitText(self.speak_textbox.get_text())
        
    # Split text by full stops, if sentence has more than 30 words throw an error
    def splitText(self, message):
        phrase = ""
        for sentence in message.split("."):
            if sentence.count(" ") > 3 :
                print "please add full stops"
            phrase += "\'(SayText \"%s\")\'" %(sentence)
        self.sayText(phrase)
    
    # Speak text using festival
    def sayText(self, message):
        process = Popen("festival %s" %(message) '(quit)', shell=True)
        
    def __init__(self):
        self.frame = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.frame.set_position(gtk.WIN_POS_CENTER) # Center App
        self.frame.set_size_request(600,600)
        self.frame.set_tooltip_text("Dialogue Creator") # Hover over main frame
        
        # Combo box
        self.combo = gtk.combo_box_entry_new_text()
        self.combo.connect("changed", self.combo_text)
        self.combo.append_text("Some text")
        self.combo.append_text("Some other text")
        
        # Textbox
        self.textbox = gtk.Entry()
        self.textbox.connect("changed", self.changedText)
        self.textbox.set_text("Change the title bar")
        
        # Speak textbox
        self.speak_textbox = gtk.Entry()
        self.speak_textbox.set_text("Words to be spoken really long phrase that can be tested easily bleh")
        
        # Exit button
        self.exit_button = gtk.Button("EXIT")
        self.exit_button.connect("clicked", self.killApp)
        self.exit_button.set_tooltip_text("Exit the application")
        
        # Hide button
        self.hide_button = gtk.Button("Hide")
        self.hide_button.connect("clicked", self.hideTextbox)
        
        # Show button
        self.show_button = gtk.Button("Show")
        self.show_button.connect("clicked", self.showTextbox)
        
        # Speak button
        self.speak_button = gtk.Button("Speak")
        self.speak_button.connect("clicked", self.speakText)
        self.speak_button.set_tooltip_text("Click this to speak words")
        
        # Upload image
        self.imageOne = gtk.Image()
        self.imageOne.set_from_file("images/python-logo.png")
        
        # Choose file
        self.file_button = gtk.FileChooserButton("Choose file", None)
        
        # Horizontal box
        self.container1 = gtk.HBox()
        self.container1.pack_start(self.exit_button)
        self.container1.pack_start(self.hide_button)
        self.container1.pack_start(self.show_button)
        self.container1.pack_start(self.speak_button)
        
        # Vertical box
        self.container2 = gtk.VBox()
        self.container2.pack_start(self.container1)
        self.container2.pack_start(self.textbox)
        self.container2.pack_start(self.speak_textbox)
        self.container2.pack_start(self.combo)
        self.container2.pack_start(self.imageOne)
        self.container2.pack_start(self.file_button)
        
        self.frame.set_title("Dialogue Creator 2012 - SE206 Assignment 3")
        self.frame.add(self.container2)
        self.frame.show_all() # Make all elements
        self.frame.connect("destroy", self.killApp) # Kill App when closed
        
    def main(self):
        gtk.main() # Run the main function of GTK
        
if __name__ == "__main__":
    base = Main() # Run class Base
    base.main()
